<template>
  <div>
  <div class="background">
    <img :src="imgSrc" width="100%" height="100%" alt="" />
  </div>
  <div class="login">
    <div>
      <el-form
          v-loading="loading"
          element-loading-text="正在登录..."
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
          :rules="rules"
          ref="loginForm"
          :model="loginForm"
          class="loginContainer"
      >
        <h1 class="loginTitle">Welcome</h1>

        <el-form-item prop="username" >
          <el-input
              type="text"
              auto-complete="false"
              v-model="loginForm.username"
              placeholder="username"

          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
              type="password"
              auto-complete="false"
              v-model="loginForm.password"
              placeholder="password"

          ></el-input>
        </el-form-item>

        <el-form-item prop="code">
          <el-input
              size="normal"
              type="text"
              auto-complete="false"
              v-model="loginForm.code"
              placeholder="click on"
              @keyup.enter.native="submitLogin()"
              style="width: 250px; margin-right: 5px"
          ></el-input>
          <img :src="captchaUrl" @click="updateCaptcha" />
        </el-form-item>

        <el-button type="info" plain style="width: 100%"  @click="submitLogin" @keyup.enter="submitLogin"
        >Login</el-button
        >
<!--        <el-button type="primary" style="right: 50%" @click="register">注册</el-button-->
<!--        >-->
        <el-form-item style="width: 100%">
          <a @click="register" class="register">Register</a>
        </el-form-item>
      </el-form>
    </div>
  </div>
  </div>
</template>


<script>

import {getRequest} from "@/utils/api";
export default {
  name: "Login",
  data() {
    return {
      imgSrc:require('../img/R-C.jpg'),
      captchaUrl: "/captcha?time=" + new Date(),
      loginForm: {
        username: "",
        password: "",
        code: "",
      },
      loading: false,
      checked: true,
      rules: {
        username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
      },
    };
  },
  methods: {
    updateCaptcha() {
      this.captchaUrl = "/captcha?time=" + new Date();
    },
    register() {
      this.$router.push('/Register')
    },
    submitLogin() {
      this.loading = true;
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.postRequest('/login',this.loginForm).then(resp=>{
            if(resp?.code==200){
              this.loading = false;
              //存储用户token
              const tokenStr = resp.data.tokenHead+resp.data.token;
              window.sessionStorage.setItem("tokenStr", tokenStr);//08登录跳转有隐患，key值为NaN

              getRequest('/manager/info').then(resp=>{
                window.sessionStorage.setItem("username", resp.username);
                if (resp?.auths=="admin") {
                  this.$router.replace('/AdminPage');
                }else {
                  this.$router.replace('/UserHome');}
              })
            }else {
              this.loading = false;
              // this.$message.error(resp?.message);
              this.updateCaptcha();
            }
          })
          // this.loading = true;
          // this.postRequest("/login", this.loginForm).then((resp) => {
          //   if (resp=="登录成功") {
          //     this.loading = false;
          //     this.$message.success(resp);
          //     //跳转首页
          //     //this.$router.replace('/HomePage')
          //     //跳转登录页面后，登录直接进入指定页面
          //     let path = this.$route.query.redirect;
          //     this.$router.replace((path== '/'||path==undefined)?'/HomePage':path);
          //   } else {
          //     this.loading = false;
          //     this.$message.error(resp);
          //     this.updateCaptcha();
          //   }
          // });
        } else {
          this.loading = false;
          this.$message.error("请输入全部信息！");
          return false;
        }
      });
    },
  },
};
</script>


<style >
    .el-textarea__inner,.el-input__inner{
        background-color: transparent !important;;
    }
.background {
  width:100%;
  height:100%;  /**宽高100%是为了图片铺满屏幕 **/
  z-index:-1;
  position: absolute;
}
/*.login {*/
/*  !*background-color: antiquewhite;*!*/
/*  !*background-image: url("../assets/login.jpg");*!*/
/*  background-size: 100%;*/
/*  height: auto;*/
/*}*/

.loginContainer {
  /*border-radius: 15px;*/
  /*background-clip: padding-box;*/
  /*margin: 0px auto;*/
  /*width: 358px;*/
  /*padding: 0px 35px 15px 35px;*/
  /*background: #8bf3f3;*/
  /*border: 1px solid #443e45;*/
  /*box-shadow: 0 0 25px #4a4b52;*/
  /*left: 36%;*/
  /*z-index:1;*/
  /*position: absolute;*/
    position:absolute;
    /*定位方式绝对定位absolute*/
    top:50%;
    left:80%;
    /*顶和高同时设置50%实现的是同时水平垂直居中效果*/
    transform:translate(-50%,-50%);
    /*实现块元素百分比下居中*/
    width:300px;
    padding:50px;
    background: rgba(0,0,0,.1);
    /*背景颜色为黑色，透明度为0.8*/
    box-sizing:border-box;
    /*box-sizing设置盒子模型的解析模式为怪异盒模型，
    将border和padding划归到width范围内*/
    box-shadow: 0px 15px 25px rgba(0,0,0,.5);
    /*边框阴影  水平阴影0 垂直阴影15px 模糊25px 颜色黑色透明度0.5*/
    border-radius:15px;
    /*边框圆角，四个角均为15px*/
}
/*   .loginContainer{*/
/*    border-radius: 6px;*/
/*    background: #ffffff;*/
/*    width: 385px;*/
/*    padding: 25px 25px 5px 25px;*/
/*}*/


.loginTitle {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #a5a5a7;
}
.el-form-item__content{
  display: flex;
  align-items: center;
}
.register {
  float: right;
  color: wheat;
}

</style>